<template>
  <div>
    <h1>文章列表</h1>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="_id" label="id"> </el-table-column>
      <el-table-column prop="title" label="文章标题"> </el-table-column>
      <el-table-column label="文章图片">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.url" alt="" style="height: 3em" />
          </div>
        </template> </el-table-column
      >>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="echo(scope.row._id)"
            >编辑</el-button
          >
          <el-button @click="remov(scope.row._id)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="editform" label-width="80px">
        <el-form-item label="修改标题">
          <el-input v-model="editform.title"></el-input>
        </el-form-item>
        <el-form-item label="修改名字">
          <el-input v-model="editform.name"></el-input>
        </el-form-item>
        <el-form-item label="大图">
          <el-input v-model="editform.barnn"></el-input>
        </el-form-item>
        <el-form-item label="视频">
          <el-input v-model="editform.vido"></el-input>
        </el-form-item>
        <el-form-item label="修改头像">
          <el-upload
            class="avatar-uploader"
            :action="$http.defaults.baseURL + '/upload'"
            :headers="getAuthHeaders()"
            :show-file-list="false"
            :on-success="(res) => $set(editform, 'url', res.url)"
          >
            <img v-if="editform.url" :src="editform.url" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <div>
          
          <el-button size="small" @click="editform.skills.push({})">
            <i class="el-icon-plus"></i> 添加评论人
          </el-button>
            <!-- <el-col :md="12" v-for="(item, i) in editform.skills" :key="i"> -->
              <div  v-for="(item, i) in editform.skills" :key="i">
                  <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
               <el-form-item label="头像">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
                 <el-form-item label="评论内容">
            <el-input v-model="item.title"></el-input>
          </el-form-item>
              </div>
        </div>



      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 <script>
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      editform: {
        title: "",
        _id: "",
        url: "",
        body: "",
        skills:[],
        icon:''
      },
    };
  },

  created() {
    this.getlist();
    // this.getpartList()
  },
  methods: {
    async getlist() {
      const res = await this.$http.get("/rest/vides");
      this.tableData = res.data;
      console.log(res);
    },
    // 删除
    async remov(id) {
      // 弹框提示用户，是否要删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 用户取消了删除
      if (confirmResult !== "confirm") {
        return this.$message({
          type: "info",
          message: "已取消删除",
        });
      }
      // console.log(id);
      const res = await this.$http.delete(`/rest/vides/${id}`);
      console.log(res);
      this.$message.success = "成功删除";
      this.getlist();
      // this.dialogVisible = true;
    },
    // 回显
    async echo(id) {
      this.dialogVisible = true;
      const res = await this.$http.get(`/rest/vides/${id}`);
      // console.log(res)
      this.editform = res.data;
    },

    // 修改
    async edit() {
      const res = await this.$http.put(
        `/rest/vides/${this.editform._id}`,
        this.editform
      );
      this.dialogVisible = false;
      console.log(res);
      this.getlist();
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>